// 移除内联JS并优化错误处理
document.addEventListener('DOMContentLoaded', function() {
    const updateBtn = document.getElementById('update-api-btn');
    updateBtn.addEventListener('click', async function(e) {
        e.preventDefault();
        const apiKey = document.getElementById('api_key').value;
        const statusDiv = document.getElementById('api-status');
        
        try {
            const response = await fetch('/update_api_key', {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({ api_key: apiKey })
            });
            const data = await response.json();
            
            statusDiv.textContent = data.success ? 'API密钥更新成功！' : '更新失败：' + data.message;
            statusDiv.style.color = data.success ? 'green' : 'red';
            statusDiv.style.backgroundColor = data.success ? 'rgba(0,255,0,0.1)' : 'rgba(255,0,0,0.1)';
        } catch (error) {
            statusDiv.textContent = '网络错误，请重试。';
            statusDiv.style.color = 'red';
            statusDiv.style.backgroundColor = 'rgba(255,0,0,0.1)';
        }
    });
});